const { DataTypes } = require('sequelize')
const sequelize = require('../config/database')

const Equipment = sequelize.define('Equipment', {
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true
  },
  code: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true
  },
  name: {
    type: DataTypes.STRING,
    allowNull: false
  },
  type: {
    type: DataTypes.STRING,
    allowNull: false
  },
  model: {
    type: DataTypes.STRING,
    allowNull: true
  },
  manufacturer: {
    type: DataTypes.STRING,
    allowNull: true
  },
  purchaseDate: {
    type: DataTypes.DATE,
    allowNull: true
  },
  location: {
    type: DataTypes.STRING,
    allowNull: true
  },
  status: {
    type: DataTypes.ENUM('running', 'idle', 'maintenance', 'broken'),
    defaultValue: 'idle'
  },
  capacity: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: true,
    comment: '设备产能'
  },
  efficiency: {
    type: DataTypes.DECIMAL(5, 2),
    defaultValue: 100,
    comment: '设备效率(%)'
  },
  lastMaintenance: {
    type: DataTypes.DATE,
    allowNull: true
  },
  nextMaintenance: {
    type: DataTypes.DATE,
    allowNull: true
  },
  runtime: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '累计运行时间(小时)'
  },
  parameters: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: '设备参数'
  },
  notes: {
    type: DataTypes.TEXT,
    allowNull: true
  },
  isActive: {
    type: DataTypes.BOOLEAN,
    defaultValue: true
  }
}, {
  tableName: 'equipment',
  timestamps: true
})

module.exports = Equipment
